package com.redsoft.spirit.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.redsoft.spirit.entity.ComCodeTable;
import com.redsoft.spirit.entity.JsonResult;
import com.redsoft.spirit.entity.TreeNode;
import com.redsoft.spirit.service.ComCodeTableService;

@RestController
public class CodeTableController {

	@Autowired
	private ComCodeTableService codeTableService;

	private Gson gson = new Gson();

	/**
	 * 查询码表的codeTable树
	 * 
	 * <pre>
	 * 查询码表的codeTable树
	 * </pre>
	 * 
	 * @author 杨雷
	 * @return
	 */
	@GetMapping("/codeTableTree")
	public String codeTableTree() {

		List<TreeNode> tree = codeTableService.codeTableTree();
		JsonResult result = new JsonResult(tree);

		return result.success().toString();
	}

	@GetMapping("/codeTableTree/{id}")
	public String moudlePages(@PathVariable String id) {
		List<TreeNode> tree = new ArrayList<>();

		List<ComCodeTable> tables = codeTableService.getByParentId(id);

		for (ComCodeTable codeTable : tables) {
			TreeNode node = new TreeNode();
			node.setId(codeTable.getId());
			node.setLabel(codeTable.getItemName());

			tree.add(node);
		}
		return new JsonResult(tree).success().toString();
	}

	/**
	 * 根据id查询码表具体内容.
	 * 
	 * <pre>
	 * 根据id查询码表具体内容
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param id
	 * @return
	 */
	@GetMapping("/codeTables/{id}")
	public String codeTableTree(@PathVariable String id) {
		ComCodeTable codeTable = codeTableService.getById(id);

		List<ComCodeTable> sons = codeTableService.getByParentInnerName(codeTable.getInnerName());

		Map map = new HashMap<>();
		map.put("codeTable", codeTable);
		map.put("children", sons);
		return new JsonResult(map).success().toString();
	}

	/**
	 * 新增或者保存修改码表内容
	 * 
	 * <pre>
	 * 新增或者保存修改码表内容
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@PostMapping("/codeTables")
	public String save(@RequestBody ComCodeTable codeTable) throws Exception {
		codeTableService.save(codeTable);
		return new JsonResult().success().toString();
	}

	/**
	 * 修改码表内容
	 * 
	 * <pre>
	 * 修改码表内容
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@PutMapping("/codeTables")
	public String update(@RequestBody ComCodeTable codeTable) throws Exception {
		codeTableService.save(codeTable);
		return new JsonResult().success().toString();
	}

	/**
	 * 删除此项以及所有子项
	 * 
	 * <pre>
	 * 删除此项以及所有子项
	 * </pre>
	 * 
	 * @author 杨雷
	 * @param id
	 * @return
	 */
	@DeleteMapping("/codeTables/{id}")
	public String delCodeTable(@PathVariable String id) {
		codeTableService.deleteAllById(id);
		return new JsonResult().success().toString();
	}
}